package cn.zkrt.iotms.module.iot.service.impl;

import cn.zkrt.iotms.module.iot.dao.DeviceBDao;
import cn.zkrt.iotms.module.iot.dao.DeviceTypeAttributeBDao;
import cn.zkrt.iotms.module.iot.entity.DeviceB;
import cn.zkrt.iotms.module.iot.entity.DeviceTypeAttributeB;
import cn.zkrt.iotms.module.iot.entity.DeviceTypeB;
import cn.zkrt.iotms.module.iot.dao.DeviceTypeBDao;
import cn.zkrt.iotms.module.iot.service.DeviceTypeBService;
import cn.zkrt.iotms.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备类型(DeviceTypeB)表服务实现类
 *
 * @author xulinyu
 * @since 2024-10-05 19:03:40
 */
@Service("deviceTypeBService")
public class DeviceTypeBServiceImpl extends ServiceImpl<DeviceTypeBDao,DeviceTypeB> implements DeviceTypeBService {
    @Resource
    private DeviceTypeBDao deviceTypeBDao;

    private final DeviceBDao deviceBDao;

    private final DeviceTypeAttributeBDao deviceTypeAttributeBDao;

    public DeviceTypeBServiceImpl(DeviceBDao deviceBDao, DeviceTypeAttributeBDao deviceTypeAttributeBDao) {
        this.deviceBDao = deviceBDao;
        this.deviceTypeAttributeBDao = deviceTypeAttributeBDao;
    }

    @Override
    public Page<DeviceTypeB> queryByPage(Page<DeviceTypeB> page, String deviceTypeName) {
        return deviceTypeBDao.queryByPage(page,deviceTypeName);
    }

    @Override
    public Boolean deleteByIds(List<Long> idList) {
        //如果已经关联了设备就不允许删除
        LambdaQueryWrapper<DeviceB> w1 = new LambdaQueryWrapper<>();
        w1.in(DeviceB::getDeviceTypeId,idList);
        Integer count = deviceBDao.selectCount(w1);
        if(count > 0) {
            throw new RuntimeException("请解除设备类型关联");
        }
        //删除设备类型属性
        LambdaQueryWrapper<DeviceTypeAttributeB> w2 = new LambdaQueryWrapper<>();
        w2.in(DeviceTypeAttributeB::getDeviceTypeId,idList);
        deviceTypeAttributeBDao.delete(w2);
        //删除本身
        return this.removeByIds(idList);
    }

    @Override
    public Boolean add(DeviceTypeB deviceTypeB) {
        verifyDuplicate(deviceTypeB);
        return this.save(deviceTypeB);
    }

    @Override
    public Boolean edit(DeviceTypeB deviceTypeB) {
        verifyDuplicate(deviceTypeB);
        return this.updateById(deviceTypeB);
    }


    /**
     * 校验标识符重复
     * @param deviceTypeB
     */
    public void verifyDuplicate(DeviceTypeB deviceTypeB) {
        //校验标识符重复
        LambdaQueryWrapper<DeviceTypeB> w1 = new LambdaQueryWrapper<>();
        w1.ne(StringUtils.isNotEmpty(deviceTypeB.getId()),DeviceTypeB::getId,deviceTypeB.getId());
        w1.eq(StringUtils.isNotEmpty(deviceTypeB.getDeviceTypeKey()),DeviceTypeB::getDeviceTypeKey,deviceTypeB.getDeviceTypeKey());
        int count = this.count(w1);
        if(count > 0) {
            throw new RuntimeException("设备类型标识符已存在");
        }
    }
}
